package com.tanker.common.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public final class DateUtil {

	/**
	 * 前端显示格式化
	 * @param period 时间间隔 秒
	 */
	public static String calUseTime(long period) {
		String ret = "";
		if (period > 0) {
			if (period <= 120 * 60) {
				long m =  period / 60;
				if(period % 60 > 0)
					m = m + 1;
				ret = m + "分钟";
			} else if (period > 120 * 60 && period <= 24 * 60 * 60) {
				long h = period / (60 * 60);
				long m = (period - h * 60 * 60) / 60;
				if((period - h * 60 * 60) % 60>0)
					m = m + 1;
				ret = h + "小时" + m + "分钟";
			} else if (period > 24 * 60 * 60) {
				long d = period / (24 * 60 * 60);
				long h = (period - d * 24 * 60 * 60) / (60 * 60);
				long m = (period - d * 24 * 60 * 60 - h * 60 * 60) / 60;
				if((period - d * 24 * 60 * 60 - h * 60 * 60) % 60 > 0)
					m = m + 1;
				ret = d + "天" + h + "小时" + m + "分钟";
			}
		}
		return ret;
	}

	/**
	 * 计算时间间隔
	 * @param start
	 * @param end
	 */
	public static long calTimeStamp(long start, long end) {
		long ret = 0;
		if (end > start) {
			try {
				Date d1 = CommonConstants.DATETIME_SEC_STR_PARSE(String
						.valueOf(start));
				Date d2 = CommonConstants.DATETIME_SEC_STR_PARSE(String
						.valueOf(end));
				ret = (d2.getTime() - d1.getTime()) / 1000;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}

	public static String getCurDate(String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(new Date());
	}

	public static String getCurTime() {
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
		return df.format(new Date());
	}

	public static Timestamp strToTimestamp(String date, String format) {
		DateFormat f = new SimpleDateFormat(format);
		Timestamp ts = null;
		try {
			ts = new Timestamp(f.parse(date).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ts;
	}

	public static int getBetweenDayNumber(Date to, Date from) {
		if (to.after(from)) {
			long toTime = to.getTime();
			long fromTime = from.getTime();
			long difference = toTime - fromTime;
			return (int) (difference / 86400000);
		}
		return 0;
	}

	public static String date2Str(final Date date, final String format,
			String defValue) {
		String value = date2Str(date, format);
		return value == null ? defValue : value;
	}

	public static String date2Str(final Date date, final String format) {

		if (date == null) {
			return null;
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern(format);
		return dateFormat.format(date);
	}

	public static String getGMT(final Date date, final String format) {
		DateFormat df = new SimpleDateFormat(format, Locale.ENGLISH);
		df.setTimeZone(TimeZone.getTimeZone("GMT"));
		return df.format(date);
	}

	public static Date str2Date(final String s, final String format) {
		if (s == null || s.length() == 0) {
			return null;
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(format,
				Locale.ENGLISH);
		Date date = null;
		try {
			date = dateFormat.parse(s);
		} catch (Exception e) {
			date = getSysDate();
		}
		return date;
	}

	public static Date add(Date date, int field, int amount, boolean real) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.add(field, amount);

		switch (field) {
		case Calendar.YEAR:
		case Calendar.MONTH:
		case Calendar.WEEK_OF_MONTH:
		case Calendar.WEEK_OF_YEAR:
			cal.add(Calendar.DATE, real ? (amount > 0 ? -1 : 1) : 0);
			break;
		default:
		}
		return cal.getTime();
	}

	public static Date add(Date date, int field, int amount) {
		return add(date, field, amount, true);
	}

	public static Date getSysDate() {
		return new Date();
	}

	public static Integer getYear(Date date) {

		if (date == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH);
	}

	public static int getHour(Date date) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.HOUR);
	}

	public static int getMinute(Date date) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.SECOND);
	}

	private DateUtil() {

	}

	private static SimpleDateFormat yMd_sdf = new SimpleDateFormat("yyyy-MM-dd");

	public static int getDifferDays(String strBegin, String strEnd) {
		try {
			return getDifferDays(yMd_sdf.parse(strBegin), yMd_sdf.parse(strEnd));
		} catch (ParseException e) {
			return 0;
		}
	}

	public static int getDifferDays(Date begin, Date end) {
		return (int) ((end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000));
	}
}
